#include <http.h>
#include "publictransporttilerequest.h"
#include <aknglobalnote.h> 
#include <aknnotewrappers.h> 
#include "FunTrace.h"

static void PrintNoteL(TRefByValue<const TDesC> aFormat, ...)
    {
    VA_LIST list;
    VA_START(list, aFormat);
    TBuf<256> buf;
    buf.FormatList(aFormat, list);
    VA_END(list);
	CAknGlobalNote* globalNote = CAknGlobalNote::NewLC();
	globalNote->ShowNoteL(EAknGlobalInformationNote, buf); 
    CleanupStack::PopAndDestroy(globalNote);
    }

void CPublicTransportTileRequest::ConstructL(CPublicTransportTileSession* aSession)
{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	iSession = aSession;
	iSession->iRequests.AppendL(this);
	iBody.CreateL(30*1024); // to start with (the buffer will grow accordingly)
}

CPublicTransportTileRequest* CPublicTransportTileRequest::NewLC(CPublicTransportTileSession* aSession)
{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	CPublicTransportTileRequest* ret = new (ELeave) CPublicTransportTileRequest;
	CleanupStack::PushL(ret);
	ret->ConstructL(aSession);
	return ret;
}

CPublicTransportTileRequest* CPublicTransportTileRequest::NewL(CPublicTransportTileSession* aSession)
{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	CPublicTransportTileRequest* ret = NewLC(aSession);
	CleanupStack::Pop(ret);
	return ret;
}

void CPublicTransportTileRequest::SubmitL(int aZoom, int aTileX, int aTileY, MPublicTransportTileCallback* aTarget, void* aUserData)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	
	iTarget = aTarget;
	iUserData = aUserData;
	iZoom = aZoom;
	iTileX = aTileX;
	iTileY = aTileY;
	
	RBuf8 uristr;
	CleanupClosePushL(uristr);
	uristr.CreateL(256);
#if defined(MAPQUEST)
	uristr.Format(_L8("http://otile1.mqcdn.com/tiles/1.0.0/osm/%d/%d/%d.png"), 11+iZoom, iTileX, iTileY);
#elif defined(ONEMAP)
	uristr.Format(_L8("http://cf1.onemap.sg/BASEMAP/Layers/_alllayers/L%02d/R%08x/C%08x.jpg"), iZoom, iTileY, iTileX);
#endif
	TUriParser8 uri;
	uri.Parse( uristr );
	RStringF method = iSession->iHTTPSession.StringPool().StringF(HTTP::EGET,RHTTPSession::GetTable());
	CleanupClosePushL(method);
	if(iTransaction != RHTTPTransaction())
		{
		iTransaction.Cancel();
		iTransaction.Close();
		}
	iTransaction = iSession->iHTTPSession.OpenTransactionL(uri, *this, method);
	
	{
		// server does not support pipelining
		RStringPool strP = iSession->iHTTPSession.StringPool();
		const TStringTable& stringTable = RHTTPSession::GetTable();
		RStringF httpPipelining = strP.StringF( HTTP::EHttpPipelining, stringTable );
		THTTPHdrVal disablePipelining = strP.StringF( HTTP::EDisablePipelining, stringTable );
		iTransaction.PropertySet().RemoveProperty( httpPipelining );
		iTransaction.PropertySet().SetPropertyL( httpPipelining, disablePipelining );
	}
	
	
	iBody.Zero();
	iLastError = KErrNone;
	iTransaction.SubmitL();
	CleanupStack::PopAndDestroy(&method);
	CleanupStack::PopAndDestroy(&uristr);
	
	}

void CPublicTransportTileRequest::Cancel()
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	if(iTransaction != RHTTPTransaction())
		{
		iTransaction.Cancel();
		iTransaction.Close(); // POWERDOWN
		iTransaction = RHTTPTransaction();
		}
	}

CPublicTransportTileRequest::~CPublicTransportTileRequest()
{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	Cancel();
	TInt i;
	for(i = 0 ; i < iSession->iRequests.Count() && iSession->iRequests[i] != this ; ++i);
	if(i < iSession->iRequests.Count())
		iSession->iRequests.Remove(i);
	iBody.Close();
}

void CPublicTransportTileRequest::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	if(aEvent.iStatus < KErrNone) iLastError = aEvent.iStatus;

	  switch (aEvent.iStatus)
	    {
			case THTTPEvent::EUnrecoverableError:
				iLastError = KErrNotReady; // for Symbian 5th ed + 
			  break;
			  
	    case THTTPEvent::EGotResponseHeaders:
	      break;

	    case THTTPEvent::EGotResponseBodyData:
	    	{
		      MHTTPDataSupplier* body = aTransaction.Response().Body();
		      TPtrC8 part;	      
		      body->GetNextDataPart(part);
		      
		      if(part.Length() + iBody.Length() > iBody.MaxLength() )
		    	  iBody.ReAllocL(part.Length() + iBody.Length());	    	  	    	  
		      
	    	  iBody.Append(part);	    	  	    	  
		      body->ReleaseData();
	    	}		      
	      break;
	      
	    case THTTPEvent::ESucceeded:
	    case THTTPEvent::EFailed:
	    	{
	    	
	    	TInt statusCode = aTransaction.Response().StatusCode();
	    	iTransaction = RHTTPTransaction();
	    	iTarget->MPublicTransportTileCallbackRunL(this, iZoom, iTileX, iTileY, aEvent.iStatus, iLastError, statusCode, iBody, iUserData);
	    	aTransaction.Cancel();
	    	aTransaction.Close(); // note: if a leave is raised before reaching this line, MHFRunError will close the transaction
	    	}
	      break;

	    case THTTPEvent::ERedirectedPermanently:
	      break;
	    	
	    case THTTPEvent::ERedirectedTemporarily:
	      break;
	    	
	    default:
	      break;
	    }
	}

extern TInt sLastLine;

TInt CPublicTransportTileRequest::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	return iTarget->MPublicTransportTileCallbackRunError(this, iZoom, iTileX, iTileY, aError, iBody, iUserData);
	}


MPublicTransportTileCallback::~MPublicTransportTileCallback()
	{	
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	}


CPublicTransportTileSession::CPublicTransportTileSession()
	{
	
	}
CPublicTransportTileSession* CPublicTransportTileSession::NewL()
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	CPublicTransportTileSession* ret = NewLC();
	CleanupStack::Pop();
	return ret;
	}
CPublicTransportTileSession* CPublicTransportTileSession::NewLC()
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	CPublicTransportTileSession* ret = new (ELeave) CPublicTransportTileSession;
	CleanupStack::PushL(ret);
	ret->ConstructL();
	return ret;
	}
void CPublicTransportTileSession::ConstructL()
	{
	}
CPublicTransportTileSession::~CPublicTransportTileSession()
	{
	
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	for(TInt i = 0 ; i < iRequests.Count() ; ++i)
		delete iRequests[i];
	iRequests.Close();
	iHTTPSession.Close();
	}
void CPublicTransportTileSession::SetConnectionInfoL(const RConnection& aConnection, const RSocketServ& aSocketServ)
{
TFunTrace funTrace(__PRETTY_FUNCTION__);
iHTTPSession.OpenL();
RStringPool strP = iHTTPSession.StringPool();
RHTTPConnectionInfo connInfo = iHTTPSession.ConnectionInfo();
connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), THTTPHdrVal (aSocketServ.Handle()) );
TInt connPtr = REINTERPRET_CAST(TInt, &aConnection);
connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable() ), THTTPHdrVal (connPtr) );


}
void CPublicTransportTileSession::CloseConnectionL()
{
TFunTrace funTrace(__PRETTY_FUNCTION__);
for(TInt i = 0 ; i < iRequests.Count() ; ++i)
	iRequests[i]->Cancel();
iHTTPSession.Close();
}
void CPublicTransportTileSession::NotifyConnected()
{

}

